<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择接口状态" clearable>
          <el-option
            v-for="dict in interface_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="请求类型" prop="method">
        <el-select v-model="queryParams.method" placeholder="请选择请求类型" clearable>
          <el-option
            v-for="dict in interface_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['interfaceinfo:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['interfaceinfo:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['interfaceinfo:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['interfaceinfo:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="接口地址" align="center" prop="url" />
      <el-table-column label="请求头" align="center" prop="requestHeader" />
      <el-table-column label="响应头" align="center" prop="responseHeader" />
      <el-table-column label="接口状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="interface_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="请求类型" align="center" prop="method">
        <template #default="scope">
          <dict-tag :options="interface_type" :value="scope.row.method"/>
        </template>
      </el-table-column>
      <el-table-column label="请求参数" align="center" prop="requestParams" :show-overflow-tooltip="true" />
      <el-table-column label="创建人" align="center" prop="userName"/>

      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['interfaceinfo:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['interfaceinfo:info:remove']">删除</el-button>
          <el-button link type="primary" icon="link" @click="invoke(scope.row)"  v-hasPermi="['interfaceinfo:info:add']">调用</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接口信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body >
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px" :disabled="cb.value == 3 ? 'true' : 'fasle'">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="接口地址" prop="url">
          <el-input v-model="form.url" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="请求头" prop="requestHeader">
          <el-input v-model="form.requestHeader" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="请求参数" prop="requestParams">
          <el-input v-model="form.requestParams" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="响应头" prop="responseHeader">
          <el-input v-model="form.responseHeader" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="接口状态" prop="status" v-if="cb == 2">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in interface_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="请求类型" prop="method">
          <el-select v-model="form.method" placeholder="请选择请求类型">
            <el-option
              v-for="dict in interface_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <el-form-item label="在线测试"  v-if="cb == 3" >
        <el-input v-model="form.test" placeholder="请输入测试数据" />
      </el-form-item>
      <el-form-item label="测试结果"  v-if="cb == 3 " >
        <el-input v-model="invokeResult" type="textarea" />
      </el-form-item>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if = 'cb !=3 '>确 定</el-button>
          <el-button @click="realinvoke()" type="warning" v-if = "cb == 3">请 求</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo ,invokeinfo } from "@/api/interfaceinfo/info";
import useUserStore from "../../../store/modules/user.js";
import {listUser} from "../../../api/system/user.js";

const { proxy } = getCurrentInstance();
const { interface_type,interface_status } = proxy.useDict('interface_type','interface_status');
const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    description: null,
    url: null,
    status: null,
    method: null,
  },
  rules: {
    name: [
      { required: true, message: "名称不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "描述不能为空", trigger: "blur" }
    ],
    url: [
      { required: true, message: "接口地址不能为空", trigger: "blur" }
    ],
    requestHeader: [
      { required: true, message: "请求头不能为空", trigger: "blur" }
    ],
    responseHeader: [
      { required: true, message: "响应头不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "接口状态不能为空", trigger: "change" }
    ],
    method: [
      { required: true, message: "请求类型不能为空", trigger: "change" }
    ],
    requestParams: [
      { required: true, message: "请求参数不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询接口信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// const map = new Map();
// function getUserList(){
//   listUser().then(response => {
//      const userList = response.rows;
//       userList.forEach(item => {
//         map.set(item.userId,item.userName)
//       })
//
//
//   });
// }
// 取消按钮

function invoke(row){
  reset();
  invokeResult.value = '';
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    cb.value = 3; //修改
    title.value = "调用";
  });

}
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    description: null,
    url: null,
    requestHeader: null,
    responseHeader: null,
    status: null,
    method: null,
    userId: null,
    createTime: null,
    updateTime: null,
    isDelete: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

const cb = ref(0)
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  cb.value = 1;//添加
  title.value = "添加接口信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    cb.value = 2; //修改
    title.value = "修改接口信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除接口信息编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('interfaceinfo/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

const invokeResult = ref({})
const  aa= ref(0)
function realinvoke(){
  invokeinfo({
      id:form.value.id,
      userRequestParams:form.value.test
    }).then(response => {
      proxy.$modal.msgSuccess("调用成功");
      aa.value = 1;
      invokeResult.value =  response.data;
    });
}
    getList();



</script>
